package com.scalar.chiptrack.operations.dataentry.boardInventory.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.boardInventory.forms.BoardInventoryDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: ganesh
 * Date: Sep 3, 2007
 * Time: 5:08:31 PM
 */
public class BoardInventoryDataEntrySaveCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/boardInvDataEntry.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

       BoardInventoryDataEntryForm invDataEntryForm = ( BoardInventoryDataEntryForm ) form;

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);


        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if ( user.isActionAllowed( Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else if ( user.isActionAllowed( Functionality.BOARD_INVENTORY_DATA_ENTRY , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        ArrayList invWipList = new ArrayList();
        ArrayList processTypeList;

        LotDetails invLotDetails = invDataEntryForm.getInvLotDetails();
        invLotDetails.setProcessType( invDataEntryForm.getProcessType());
        invLotDetails.setProcessStepId(invDataEntryForm.getProcessStepId());
        invLotDetails.setLocationId(invDataEntryForm.getLocationId());
        invLotDetails.setLotId(invDataEntryForm.getLotId());



        if (invLotDetails.getWaferQtyOut() != null && invLotDetails.getWaferQtyOut().trim().length() <= 0)
        {
            invLotDetails.setWaferQtyOut(null);
        }
        if (invLotDetails.getDateOut() != null && invLotDetails.getDateOut().trim().length() <= 0 )
        {
            invLotDetails.setDateOut(null);
        }
        if ( invLotDetails.getDieQtyOut() != null && invLotDetails.getDieQtyOut().trim().length() <= 0 )
        {
            invLotDetails.setDieQtyOut(null);
        }
        if (invLotDetails.getWaferQtyIn() != null && invLotDetails.getWaferQtyIn().trim().length() <= 0)
        {
            invLotDetails.setWaferQtyIn(null);
        }
        if ( invLotDetails.getDieQtyIn() != null && invLotDetails.getDieQtyIn().trim().length() <= 0 )
        {
            invLotDetails.setDieQtyIn(null);
        }
        if( invLotDetails.getFabLotNumber() == null )
        {
            invLotDetails.setFabLotNumber( invDataEntryForm.getFabLotNo());
        }

        invWipList.add( invLotDetails );

        if( invWipList != null && invWipList.size() > 0  && ("Finished Goods".equalsIgnoreCase(invLotDetails.getProcessType() )|| ("DIE BANK".equalsIgnoreCase(invLotDetails.getProcessType() ))))
        {
            try
            {
                DataEntryDAO.saveDataEntryInventoryLot( null, invWipList, user.getUserName() );
                info.put ( "Lot Information Saved", null );
            }
            catch( Exception e )
            {
                e.printStackTrace();
                errors.put( "Error storing the Inventory Lot information", null );
            }
        }
        if( invWipList != null && invWipList.size() > 0  && ("ASSEMBLY".equalsIgnoreCase(invLotDetails.getProcessType() )))
        {
            try
            {
                System.out.println("inner to aasembly " );
                DataEntryDAO.saveDataEntryAssemblyLot(null, invWipList, user.getUserName() );
                info.put ( "Lot Information Saved", null );
            }
            catch( Exception e )
            {
                e.printStackTrace();
                errors.put( "Error storing the Board Assmebly Inventory   Lot information", null );
            }
        }

        if( invWipList != null && invWipList.size() > 0  && ("test".equalsIgnoreCase(invLotDetails.getProcessType() )))
        {
            try
            {
                DataEntryDAO.saveDataEntryTestLot(null, invWipList, user.getUserName() );
                info.put ( "Lot Information Saved", null );
            }
            catch( Exception e )
            {
                e.printStackTrace();
                errors.put( "Error storing the Board Test  Inventory   Lot information", null );
            }
        }
        invDataEntryForm.setProcessType("0");
        invDataEntryForm.setProcessStepId("0");
        invDataEntryForm.setLocationId("0");
        invDataEntryForm.setLotId("0");
        invDataEntryForm.setFabLotNo("0");
        processTypeList = invLotDetails.getBoardInventoryProcessList();
        request.setAttribute("processTypeList", processTypeList);
        request.setAttribute("action", action);

        return mapping.findForward( "boardInvDataEntry" );
    }
}

